package com.ymq.modle;

import com.ymq.service.FreshThread;
import com.ymq.service.Sound;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Dinosaur {
    public BufferedImage image;          // 主图片
    private BufferedImage image1,image2,image3;  //跑步图片
    public int x,y;                            // 坐标
    private int jumpValue=0;        //跳跃增量
    private boolean jumpState=false;   //跳跃状态
    private  int stepTimer=0;     // 踏步计时器
    private final int JUMP_HIGHT=100;
    private  final int LOWEST_Y=120;
    private  final  int FREASH= FreshThread.FREASH;//刷新时间


    public Dinosaur() {
        x = 50;
        y = LOWEST_Y;
        try {
            image1 = ImageIO.read(new File("image/恐龙1.png"));
            image2 = ImageIO.read(new File("image/恐龙2.png"));
            image3 = ImageIO.read(new File("image/恐龙3.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 移动
     */
    public void move() {
        step();
        if (jumpState) {//
            if (y >= LOWEST_Y) {//
                jumpValue = -4;//
            }
            if (y <= LOWEST_Y - JUMP_HIGHT) {//
                jumpValue = 4;//
            }
            y += jumpValue;//
            if (y >= LOWEST_Y) {
                jumpState = false;
            }
        }
    }
    //跳跃
    public void jump() {
        if (!jumpState) {//
            Sound.jump();//
        }
        jumpState = true;//
    }
    //踏步
    private void step() {

        int tmp = stepTimer / 250 % 3;
        switch (tmp) {
            case 1 :
                image = image1;
                break;
            case 2 :
                image = image2;
                break;
            default :
                image = image3;
        }
        stepTimer += FREASH;//
    }


    public Rectangle getFootBounds() {
        return new Rectangle(x + 30, y + 59, 29, 18);
    }

    public Rectangle getHeadBounds() {
        return new Rectangle(x + 66, y + 25, 32, 22);
    }
}
